package application.mvc.sys.config.log;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import application.common.model.StoreLog;
import application.common.render.StoreLogProperty;
import application.common.ui.ProgressStage;
import application.common.util.LayerUtil;
import application.common.util.RouteUtil;
import application.common.util.ShiroUtil;
import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.RotateTransition;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.util.Duration;

public class LogController implements Initializable{

	@FXML
	private Label headerLabel;
	
	@FXML
	private Label copyrightLabel;
	
	@FXML
	private Label usernameLabel;
	
	@FXML
	private Label roleNameLabel;
	
	@FXML
	private ImageView portraitImageView;

	@FXML
	private TableView< StoreLogProperty> logTableView;
	
	@FXML
	private Pane timerOutLinePane;

	@FXML
	private Pane timerInLinePane;

	@FXML
	private Label timerLabel;
	private Timeline animation;
	private RotateTransition rotateTransitionOut;
	private RotateTransition rotateTransitionIn;

	private final ObservableList<StoreLogProperty> data = FXCollections.observableArrayList();
	private boolean isDestroy=false;
	@SuppressWarnings("unchecked")
	public void initialize(URL location, ResourceBundle resources) {
		
		headerLabel.setText(ShiroUtil.getSoftName());
		copyrightLabel.setText(ShiroUtil.getCopyright());
		
		if(ShiroUtil.getUser()!=null) {
			portraitImageView.setImage(ShiroUtil.getUser().getPortrait());
			usernameLabel.setText(ShiroUtil.getUser().getName());
			roleNameLabel.setText(ShiroUtil.getUser().getRoleName());
		}
		logTableView.setEditable(true);

		TableColumn<StoreLogProperty, String> storeColumn = new TableColumn<StoreLogProperty, String>("编号");
		storeColumn.setMinWidth(150);
		storeColumn.setCellValueFactory(new PropertyValueFactory<StoreLogProperty, String>("storeId"));
		storeColumn.setVisible(false);

		TableColumn<StoreLogProperty, String> cabinetNameColumn = new TableColumn<StoreLogProperty, String>("柜子");
		cabinetNameColumn.setMinWidth(150);
		cabinetNameColumn.setCellValueFactory(new PropertyValueFactory<StoreLogProperty, String>("cabinetName"));

		TableColumn<StoreLogProperty, String> statusNameColumn = new TableColumn<StoreLogProperty, String>("类型");
		statusNameColumn.setMinWidth(150);
		statusNameColumn.setCellValueFactory(new PropertyValueFactory<StoreLogProperty, String>("statusName"));

		TableColumn<StoreLogProperty, String> userNameColumn = new TableColumn<StoreLogProperty, String>("操作人");
		userNameColumn.setMinWidth(150);
		userNameColumn.setCellValueFactory(new PropertyValueFactory<StoreLogProperty, String>("userName"));
		
		TableColumn<StoreLogProperty, String> createTimeColumn = new TableColumn<StoreLogProperty, String>("时间");
		createTimeColumn.setMinWidth(250);
		createTimeColumn.setCellValueFactory(new PropertyValueFactory<StoreLogProperty, String>("createTime"));
		

		logTableView.getColumns().addAll(storeColumn, cabinetNameColumn, statusNameColumn, userNameColumn,createTimeColumn);
		logTableView.setItems(data);
		
		ProgressStage.of(RouteUtil.mainStage, new Task<Void>() {
			@Override
			protected Void call() throws Exception {
				Platform.runLater(new Runnable() {
					public void run() {
						List<StoreLog> storeLogs = StoreLog.dao
								.find("select * from " + StoreLog.TABLE_NAME );
						if (storeLogs != null && storeLogs.size() > 0) {
							for (StoreLog storeLog : storeLogs) {
								data.add(new StoreLogProperty(storeLog));
							}
						}

						logTableView.setItems(data);
						rotateTransitionOut = new RotateTransition(Duration.seconds(10), timerOutLinePane);
						rotateTransitionOut.setFromAngle(0);
						rotateTransitionOut.setToAngle(360);
						rotateTransitionOut.setByAngle(360); // 设置旋转的角度
						rotateTransitionOut.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
						rotateTransitionOut.setInterpolator(Interpolator.LINEAR);
						rotateTransitionOut.play();

						rotateTransitionIn = new RotateTransition(Duration.seconds(10), timerInLinePane);
						rotateTransitionIn.setFromAngle(360);
						rotateTransitionIn.setToAngle(0);
						rotateTransitionIn.setByAngle(360); // 设置旋转的角度
						rotateTransitionIn.setCycleCount(Animation.INDEFINITE); // 设置旋转次数，我们需要旋转无数次
						rotateTransitionIn.setInterpolator(Interpolator.LINEAR);
						rotateTransitionIn.play();
						startCountDown();
					}
				});
				return null;
			}
		}, "查询日志中...").show();
	}
	
	
	public void startCountDown() {
		animation = new Timeline(new KeyFrame(Duration.millis(1000), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				if(RouteUtil.getTime()>0) {
					RouteUtil.setTime(RouteUtil.getTime() - 1);
					if (RouteUtil.getTime() == 0) {
						timerLabel.setText("0");
						LayerUtil.open("操作超时！", 999);
					} else if (RouteUtil.getTime() > 0) {
						timerLabel.setText(RouteUtil.getTime() + "");
					}
				}
			}
		}));
		animation.setCycleCount(Timeline.INDEFINITE);
		animation.play();
	}

	public void destroy() {
		if (animation != null) {
			animation.stop();
		}
		if (rotateTransitionOut != null) {
			rotateTransitionOut.stop();
		}
		if (rotateTransitionIn != null) {
			rotateTransitionIn.stop();
		}
	}

	public void exit(ActionEvent event) {
		if(!isDestroy) {
			isDestroy=true;
			Button bu = (Button) event.getSource();
			bu.setVisible(false);
			destroy();
			RouteUtil.goConfig();
		}
	}


}
